package com.xiaoman.pet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PetWindow extends JWindow {
    private final JLabel petLabel;
    private int xOffset, yOffset;

    public PetWindow() {
        // 设置窗口无边框和始终置顶
        setAlwaysOnTop(true);
        setLayout(new BorderLayout());

        // 加载宠物图片（需要准备一个pet.png放到resources目录）
        ImageIcon icon = new ImageIcon(getClass().getResource("/icons/pet.png"));
        petLabel = new JLabel(icon);
        add(petLabel, BorderLayout.CENTER);
        // 设置窗口透明
        setBackground(new Color(0, 0, 0, 0f)); // 使用浮点数设置透明度，0f表示完全透明
        // 设置窗口大小
        pack();
        // 添加拖动功能
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                xOffset = e.getX();
                yOffset = e.getY();
            }
        });
        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                setLocation(e.getXOnScreen() - xOffset, e.getYOnScreen() - yOffset);
            }
        });
        setVisible(true);
    }

    // 自动游走逻辑
    public void startRandomMove(int moveInterval) {
        new Timer(3000, e -> {
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            int newX = (int) (Math.random() * (screen.width - getWidth()));
            int newY = (int) (Math.random() * (screen.height - getHeight()));
            setLocation(newX, newY);
        }).start();
    }
}
